<?php

namespace Database\Seeders;

use App\Models\Permission;
use Illuminate\Database\Seeder;

class PermissionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 清空权限表
        Permission::query()->truncate();
        // 创建仪表盘菜单
        $dashboard = $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '仪表盘',
            'icon' => 'icon-home',
            'name' => 'admin.dashboard',
            'active' => 'admin.dashboard',
            'redirect' => 'admin.dashboard.workplace',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        // 创建工作台菜单
        $this->create([
            'parent_id' => $dashboard->id,
            'type' => Permission::TYPE_MENU,
            'title' => '工作台',
            'icon' => '',
            'name' => 'admin.dashboard.workplace',
            'active' => 'admin.dashboard',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        // 创建数据分析菜单
        $this->create([
            'parent_id' => $dashboard->id,
            'type' => Permission::TYPE_MENU,
            'title' => '数据分析',
            'icon' => '',
            'name' => 'admin.dashboard.analysis',
            'active' => 'admin.dashboard',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        // 内容
        $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '内容',
            'icon' => 'icon-topic',
            'name' => 'admin.content',
            'active' => 'admin.content',
            'redirect' => 'admin.content.articles.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

        // 店铺
        $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '店铺',
            'icon' => 'icon-curtain',
            'name' => 'admin.shop',
            'active' => 'admin.shop',
            'redirect' => 'admin.shop.merchants.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 30,
        ]);

        // 商品
        $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '商品',
            'icon' => 'icon-bread',
            'name' => 'admin.goods',
            'active' => 'admin.goods',
            'redirect' => 'admin.goods.products.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 40,
        ]);

        // 活动
        $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '活动',
            'icon' => 'icon-red-packet',
            'name' => 'admin.activity',
            'active' => 'admin.activity',
            'redirect' => 'admin.activity.red-packets.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 50,
        ]);

        // 订单
        $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '商品',
            'icon' => 'icon-order',
            'name' => 'admin.trade',
            'active' => 'admin.trade',
            'redirect' => 'admin.trade.orders.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 60,
        ]);

        // 财务
        $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '财务',
            'icon' => 'icon-wallet',
            'name' => 'admin.finance',
            'active' => 'admin.finance',
            'redirect' => 'admin.finance.wallet.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 70,
        ]);


         $setting = $this->create([
             'parent_id' => 0,
             'type' => Permission::TYPE_MENU,
             'title' => '设置',
             'icon' => 'icon-settings',
             'name' => 'admin.setting',
             'active' => 'admin.setting',
             'redirect' => 'admin.setting.website.index',
             'status' => 1,
             'is_show' => true,
             'sort' => 90,
         ]);

        $this->create([
            'parent_id' => $setting->id,
            'type' => Permission::TYPE_MENU,
            'title' => '网站设置',
            'icon' => '',
            'name' => 'admin.setting.website.index',
            'active' => 'admin.setting',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        $wechat = $this->create([
            'parent_id' => $setting->id,
            'type' => Permission::TYPE_MENU,
            'title' => '公众号',
            'icon' => 'icon-wechat',
            'name' => 'admin.setting.wechat',
            'active' => 'admin.setting',
            'redirect' => 'admin.setting.wechat.configs.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

        $this->insert([
            [
                'parent_id' => $wechat->id,
                'type' => Permission::TYPE_MENU,
                'title' => '公众号配置',
                'icon' => '',
                'name' => 'admin.setting.wechat.configs.index',
                'active' => 'admin.setting',
                'redirect' => '',
                'status' => 1,
                'is_show' => true,
                'sort' => 10,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'parent_id' => $wechat->id,
                'type' => Permission::TYPE_MENU,
                'title' => '自定义菜单',
                'icon' => '',
                'name' => 'admin.setting.wechat.menus.index',
                'active' => 'admin.setting',
                'redirect' => '',
                'status' => 1,
                'is_show' => true,
                'sort' => 20,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'parent_id' => $wechat->id,
                'type' => Permission::TYPE_MENU,
                'title' => '消息回复',
                'icon' => '',
                'name' => 'admin.setting.wechat.messages.index',
                'active' => 'admin.setting',
                'redirect' => '',
                'status' => 1,
                'is_show' => true,
                'sort' => 30,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'parent_id' => $wechat->id,
                'type' => Permission::TYPE_MENU,
                'title' => '草稿箱',
                'icon' => '',
                'name' => 'admin.setting.wechat.drafts.index',
                'active' => 'admin.setting',
                'redirect' => '',
                'status' => 1,
                'is_show' => true,
                'sort' => 40,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        $permission = $this->create([
            'parent_id' => $setting->id,
            'type' => Permission::TYPE_MENU,
            'title' => '权限',
            'icon' => 'icon-security',
            'name' => 'admin.setting.permissions',
            'active' => 'admin.setting',
            'redirect' => 'admin.setting.permissions.accounts.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

        $this->insert([
            [
                'parent_id' => $permission->id,
                'type' => Permission::TYPE_MENU,
                'title' => '账号管理',
                'icon' => '',
                'name' => 'admin.setting.permissions.accounts.index',
                'active' => 'admin.setting',
                'redirect' => '',
                'status' => 1,
                'is_show' => true,
                'sort' => 10,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'parent_id' => $permission->id,
                'type' => Permission::TYPE_MENU,
                'title' => '角色管理',
                'icon' => '',
                'name' => 'admin.setting.permissions.roles.index',
                'active' => 'admin.setting',
                'redirect' => '',
                'status' => 1,
                'is_show' => true,
                'sort' => 20,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'parent_id' => $permission->id,
                'type' => Permission::TYPE_MENU,
                'title' => '权限管理',
                'icon' => '',
                'name' => 'admin.setting.permissions.menus.index',
                'active' => 'admin.setting',
                'redirect' => '',
                'status' => 1,
                'is_show' => true,
                'sort' => 30,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * 创建权限并返回模型实例
     *
     * @param array $data
     * @return Permission
     */
    private function create(array $data): Permission
    {
        return Permission::query()->create($data);
    }

    private function insert(array $data)
    {
        return Permission::query()->insert($data);
    }
}
